package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.my.vote.MainActivity;
import com.my.vote.R;
import com.my.vote.adapter.MainAdapter;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.req.ReqVoteList;
import com.my.vote.pst.info.IPstMyVoteList;
import com.my.vote.widget.SwipeRefreshMoreLayout;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyVoteListActivity extends AbstractActivity implements IPstMyVoteList.IVVoteList,  SwipeRefreshMoreLayout.OnLoadingListener {


    public static void startUI(Context context) {
        Intent intent = new Intent(context, MyVoteListActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.tvLeft)
    TextView tvLeft;
    @BindView(R.id.base_refresh)
    SwipeRefreshMoreLayout refreshMoreLayout;

    int nowPage = 0;

    IPstMyVoteList pstVoteList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vote_list);
        ButterKnife.bind(this);
        MainAdapter adapter = new MainAdapter(this);
        refreshMoreLayout.setOnLoadingListener(this);
        refreshMoreLayout.setAdapter(adapter, true);
        refreshMoreLayout.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                EVoteInfo info = (EVoteInfo) adapter.getItem(position);
                VoteInfoActivity.startUI(MyVoteListActivity.this, info.getObjectId());
            }
        });
        refreshMoreLayout.onAutoRefresh();
    }

    @OnClick(R.id.tvLeft)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefreshUI() {
        this.nowPage = 0;
        ReqVoteList req = new ReqVoteList();
        req.setNowPage(nowPage);
        pstVoteList.listPage(req);
    }

    @Override
    public void onLoadMoreUI() {
        this.nowPage += 1;
        ReqVoteList req = new ReqVoteList();
        req.setNowPage(nowPage);
        pstVoteList.listPage(req);
    }

    @Override
    public void onPageList(int nowPage, int allSize, List<EVoteInfo> page) {
        this.nowPage = nowPage;
        this.refreshMoreLayout.onRefreshCompale(page, this.nowPage != 0);
        if (nowPage == allSize) {
            this.refreshMoreLayout.onLoadAllCompale();
        }
    }

    @Override
    public void onPageError(String error) {
        this.refreshMoreLayout.onLoadMoreFail();
        this.showToast(error);
    }

    @Override
    public  IPresenter getPresenter() {
        if (pstVoteList == null){
            pstVoteList = LoaderService.getService(IPstMyVoteList.class);
        }
        return pstVoteList;
    }
}
